package com.jmu.bishe.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONObject;
import com.jmu.bishe.entity.Member;
import com.jmu.bishe.service.UserService;
import com.jmu.bishe.service.main.TableService;
import com.jmu.bishe.web.common.BaseController;

@Controller
public class UserController extends BaseController{

	@Autowired
	private TableService service;
	
	@Autowired
	private UserService userService;

	/**
	 * 用户注册页面
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/user/register.do") 
    public String register(Model model,HttpServletRequest request) throws Exception {
        return "register";
    }
   
	/**
	 * 用户注册处理
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/user/do-register.do",method=RequestMethod.POST) 
    public String doregister(Model model,HttpServletRequest request,Member member) throws Exception {	

		userService.saveOrUpdate(member);
		return "login";
    }
	/**
	 * 用户注册页面
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/admin/edit-profile.do") 
    public String editprofile(Model model,HttpServletRequest request) throws Exception {
		Member m = getCurUser(request);
		if(m.getSex().equals("0")){
			model.addAttribute("Uname",m.getUserName()+"先生");
			
		}else if(m.getSex().equals("1")){
			model.addAttribute("Uname",m.getUserName()+"女士");
		}else model.addAttribute("Uname",m.getUserName()+"小姐");
		return "edit-profile";
    }
	/**
	 * 用户称谓修改
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/admin/editusername.do") 
    public String editusername(Model model,HttpServletRequest request) throws Exception {
        return "edit-profile";
    }
	/**
	 * 用户称谓修改处理
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/admin/editusername.do",method=RequestMethod.POST) 
    public String doeditusername(Model model,HttpServletRequest request,Member member) throws Exception {
		Member m = getCurUser(request);
		m.setUserName(member.getUserName());
		m.setSex(member.getSex());
		userService.saveOrUpdate(m);
		return "edit-profile";
    }
	/**
	 * 用户密码修改
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/admin/editpassword.do") 
    public String editpassword(Model model,HttpServletRequest request) throws Exception {
        return "edit-profile";
    }
	/**
	 * 用户密码修改处理
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/admin/editpassword.do",method=RequestMethod.POST) 
    public void doeditpassword(Model model,HttpServletRequest request,HttpServletResponse response) throws Exception {
		JSONObject json = new JSONObject();
		
		Member m = getCurUser(request);
		String oldpassword = request.getParameter("OldPassword");
		String password = request.getParameter("password");
		String ConfirmPassword = request.getParameter("ConfirmPassword");
		if(password.equals(ConfirmPassword)){
			if(oldpassword.equals(m.getPassword())){
				m.setPassword(password);
				Boolean b = userService.saveOrUpdate(m);
				json.put("status", b?"success":"error");
			}else{
				json.put("msg", "输入旧密码不正确");
			}			
		}else{
			json.put("msg", "新密码与确认密码不一致");
		}
		
		printOut(response, json.toJSONString());
    }
}
